package cn.etgps.etgps.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.etgps.etgps.R;
import cn.etgps.etgps.dataEntity.VehicleInfoBean;
import cn.etgps.etgps.port.VehicleListPort;
import cn.etgps.etgps.utils.CommonUtils;

/**
 * Created by itte on 2016/3/28.
 */
public class VehicleListAdapter extends RecyclerView.Adapter<VehicleListAdapter.ViewHolder> {
    private Context context;
    private List<VehicleInfoBean.ContentBean> data = new ArrayList<>(0);
    private VehicleListPort vehicleListPort;

    public VehicleListAdapter(Context context, VehicleListPort vehicleListPort) {
        this.context = context;
        this.vehicleListPort = vehicleListPort;
    }

    public void addItemLast(List<VehicleInfoBean.ContentBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void addItemTop(List<VehicleInfoBean.ContentBean> data) {
        if (this.data != null) {
            this.data.clear();
            this.data.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_vehicle_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        holder.vehiCleName = (TextView) view.findViewById(R.id.item_vehicle_name);
        holder.layoutView = (LinearLayout) view.findViewById(R.id.item_vehicle_layout);
        ViewGroup.LayoutParams ivParams = holder.layoutView.getLayoutParams();
        ivParams.width = (CommonUtils.getDisplayWidth(context) - 300);
        holder.layoutView.setLayoutParams(ivParams);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final VehicleInfoBean.ContentBean contentBean = data.get(position);
        String nameStr = contentBean.getLicense();
        if (!TextUtils.isEmpty(nameStr)) {
            holder.vehiCleName.setText(nameStr);
            holder.vehiCleName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (vehicleListPort != null) {
                        vehicleListPort.onSetInfoEntity(contentBean);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView vehiCleName;
        LinearLayout layoutView;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
